<?php

namespace Modules\Web\Service;

use HiCommon\Repository\ExpressRepository;
use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\StoreOrderRefundRepository;
use HiCommon\Repository\StoreOrderRepository;
use HiCommon\Repository\OrderMallRepository;
use HiCommon\Repository\AreasRepository;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class PublicService
{


    /**
     *  获取所有省份信息列表
     *  hinq 2019 11 21
     *
     **/
    public function get_address_list(array $arr_input)
    {
        $arr_input ['level'] = empty($arr_input ['level']) ? 1 : $arr_input ['level'];
        $arr_input ['id'] = empty($arr_input ['id']) ? 100000 : $arr_input ['id'];
        $arr_option['where'] = ['level' => $arr_input ['level'], 'pid' => $arr_input ['id']];
        $arr_option['order'] = ['key' => 'id', 'value' => 'asc'];
        $arr_option['field'] = ['id', 'name', 'Lng', 'Lat', 'pid'];
        $arr_list = AreasRepository::get_all((array)$arr_option);
        return hello_success('ok', $arr_list);
    }


    /**
     * 根据城市编码获取城市信息
     */
    public function get_city_infor($city)
    {
        try {
            if (empty($city)) {
                return hello_error('城市名称不能为空');
            }
            $data = AreasRepository::get_city_infor(['name' => $city]);
            if (is_null($data)) {
                return hello_error('数据不存在');
            }
            return hello_success('成功', $data->toarray());
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }

    }

    /**
     * 获取已经开通的城市
     */
    public function get_open_city()
    {
        try {
            $data = ShopRepository::get_open_city();
            if (is_null($data)) {
                return hello_error('数据不存在');
            }
            $arr_data = [];
            foreach ($data as $value) {
                $arr_data['city_' . $value['city_name']['id']] = $value;
            }

            return hello_success('成功', ['arr_data' => $data, 'deal_data' => $arr_data]);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


    /**
     * 获取省列表
     */
    public function getProvinceList()
    {
        $province = AreasRepository::get_list(['pid' => 1]);
        $list = array();
        foreach ($province as $key => $item) {
            $list[$item['id']] = $item['name'];
        }

        return $list;
    }


    /**
     * 获取列表
     */
    public function getCityAraeList(array $data)
    {
        try {

            if ($data <= 0) {
                return hello_error('城市id不能为空');
            }

            $list = AreasRepository::get_list(['pid' => $data]);

            if (is_null($list)) {
                return hello_error('数据不存在');
            }

            return hello_success('成功', $list);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }

    }

    /**
     * 获取快递公司列表
     */
    public function get_express_list()
    {
        try {
            $option['field'] = ['id', 'name'];
            $list = ExpressRepository::get_all($option)->toArray();//快递公司列表
            return hello_success('成功', $list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

    /**
     * 物流追踪
     */
    public function logistics_tracking($order_id, $type)
    {
        try {
            if ($order_id <= 0) {
                return hello_error('订单id不能为空');
            }
            $arr_option['field'] = ['id', 'xpress_id', 'delivery_no'];
            if ($type == 1) {
                $arr_option['with'] = ['getExpressInfo'];
                $res = OrderMallRepository::get_one($order_id, $arr_option);
                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['delivery_no'];
                    $delivery_type = $result['get_express_info']['type'];
                }
            } elseif ($type == 2) {
                $arr_option['with'] = ['getExpress'];
                $res = StoreOrderRepository::get_one($order_id, $arr_option);
                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['delivery_no'];
                    $delivery_type = $result['get_express']['type'];
                }
            } elseif ($type == 3) {
                $refund['with'] = ['getExpress'];
                $res = StoreOrderRefundRepository::get_one($order_id, $refund);

                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['express_no'];
                    $delivery_type = $result['get_express']['type'];
                }
            } else {
                return hello_error('订单类型错误');
            }

            if ($delivery_no && $delivery_type) {

                $info = json_decode(kuaidi_search($delivery_no, $delivery_type), true);

                if ($info['status'] == '0') {
                    return hello_success('获取成功', $info['result']);
                } else {
                    return hello_error($info['msg']);
                }
            }

            return hello_error('暂无物流信息');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

    /**
     * 生成二维码
     */
    public function create_qr_code($order_id, $type)
    {
        try {
            if ($order_id <= 0) {
                return hello_error('订单id不能为空');
            }
            if (empty($type)) {
                return hello_error('订单类型错误');
            }
            $size = 400;
            $info = \GuzzleHttp\json_encode(['type' => $type, 'order_id' => $order_id]);
            // $logo = public_path('admin/images/shop_user_default.png');
            $res = QrCode::size($size)->generate($info);

            return $res;
            // $gen = QrCode::format('png')->size($size)->merge($logo, .3, true)->generate($url);
            //$data = 'data:image/png;base64,' . base64_encode($gen);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

}